package com.CST.service.impl.cb;

import com.CST.common.api.Response;
import com.CST.common.api.ResultObject;
import com.CST.common.constant.Constants;
import com.CST.entity.cb.CusUnitArea;
import com.CST.entity.cb.CusUnitAreaExtend;
import com.CST.entity.cb.CusUnitAreaPageInfo;
import com.CST.mapper.cst.cb.CusUnitAreaMapper;
import com.CST.service.cb.ICusUnitAreaService;
import com.CST.util.HttpUtil;
import com.CST.util.PageHelperTool;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ly
 * @since 2021-12-22
 */
@Service
public class CusUnitAreaServiceImpl extends ServiceImpl<CusUnitAreaMapper, CusUnitArea> implements ICusUnitAreaService {
    
    @Autowired
    private CusUnitAreaMapper cusUnitAreaMapper;

    /**
     * 根据年份和月份归档
     * @return
     */
    @Override
    public Response listYearAndMonth() {
        // 使用一个集合对数据进行接受
        List<Map> timeList = new ArrayList<>();
        QueryWrapper<CusUnitArea> cusUnitAreaQueryWrapper = new QueryWrapper<>();
        cusUnitAreaQueryWrapper.select("DISTINCT year,month").orderByDesc("Month");
        List<CusUnitArea> cusUnitAreaList = cusUnitAreaMapper.selectList(cusUnitAreaQueryWrapper);
        for (CusUnitArea cusUnitArea : cusUnitAreaList){
            Map map = new HashMap();
            map.put("value", cusUnitArea.getYear()+"-"+cusUnitArea.getMonth());
            timeList.add(map);
        }
        return Response.success(timeList);
    }

    /**
     * 首页的分页查询
     * 可以携带时间条件
     * @return
     */
    @Override
    public ResultObject listCusUnitAreaByPage(CusUnitAreaPageInfo pageInfo) {
        if (pageInfo.getYear() == null || pageInfo.getMonth() == null){
            return ResultObject.success(new ArrayList<>());
        }
        List<CusUnitArea> cusUnitAreaList = cusUnitAreaMapper.listCusUnitAreaByFuzzy(pageInfo);
        // 处理分页
        PageInfo<CusUnitArea> cusUnitAreaPageInfo = PageHelperTool.excutePageInfo(cusUnitAreaList, pageInfo.getPageNum(), pageInfo.getPageSize());
        return ResultObject.success(cusUnitAreaPageInfo);
    }

    /**
     * 根据id更新信息
     * @param cusUnitArea
     * @return
     */
    @Override
    public Response updateCusUnitAreaById(CusUnitArea cusUnitArea) {
        setEntityValue(cusUnitArea);
        int i = cusUnitAreaMapper.updateCusUnitAreaById(cusUnitArea);
        if (i == 0){
            return Response.error("更新失败");
        }else {
            return Response.success("更新成功");
        }
    }

    /**
     * 根据id删除一个CusUnitArea
     * @param id
     * @return
     */
    @Override
    public Response deleteCusUnitAreaById(Integer id) {
        int i = cusUnitAreaMapper.deleteById(id);
        if (i == 0){
            return Response.error("删除失败");
        }else {
            return Response.success("删除成功");
        }
    }

    /**
     * 继承上个月数据到本月
     * @return
     */
    @Override
    public Response extendLastMonth(CusUnitAreaExtend cusUnitAreaExtend) {
        if (cusUnitAreaExtend.getLastMonth() == cusUnitAreaExtend.getThisMonth() || cusUnitAreaExtend.getLastYear() == cusUnitAreaExtend.getThisYear()){
            return Response.error("不能继承本月");
        }
        // 检索被继承的数据
        LambdaQueryWrapper<CusUnitArea> cusUnitAreaLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cusUnitAreaLambdaQueryWrapper.eq(CusUnitArea::getYear, cusUnitAreaExtend.getLastYear()).eq(CusUnitArea::getMonth, cusUnitAreaExtend.getLastMonth());
        List<CusUnitArea> cusUnitAreaList = cusUnitAreaMapper.selectList(cusUnitAreaLambdaQueryWrapper);
        if (cusUnitAreaList == null || cusUnitAreaList.size() == 0){
            return Response.error("上个月无数据");
        }else {
            // 先把当前年月的数据全部删除
            LambdaQueryWrapper<CusUnitArea> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(CusUnitArea::getYear, cusUnitAreaExtend.getThisYear());
            queryWrapper.eq(CusUnitArea::getMonth, cusUnitAreaExtend.getThisMonth());
            int delete = cusUnitAreaMapper.delete(queryWrapper);
            // 对数据进行年份和月份的修改
            for (CusUnitArea cusUnitArea : cusUnitAreaList){
                cusUnitArea.setYear(cusUnitAreaExtend.getThisYear());
                cusUnitArea.setMonth(cusUnitAreaExtend.getThisMonth());
                setEntityValue(cusUnitArea);
                // 插入数据
                int insert = cusUnitAreaMapper.insertEntity(cusUnitArea);
                if (insert == 0){
                    return Response.error("继承发生了一些错误");
                }
            }
        }
        return Response.success("继承成功");
    }

    @Override
    public Response insertCusUnitArea(CusUnitArea cusUnitArea) {
        Calendar cal = Calendar.getInstance();
        // 获取当前年
        Integer year = cal.get(Calendar.YEAR);
        // 获取当前月份
        Integer month = cal.get(Calendar.MONTH) + 1;
        // 设定一些默认值
        cusUnitArea.setRowStatus(0);
        cusUnitArea.setYear(year);
        cusUnitArea.setMonth(month);
        setEntityValue(cusUnitArea);
        int insert = cusUnitAreaMapper.insert(cusUnitArea);
        if (insert == 0){
            return Response.error("添加失败");
        }else {
            return Response.success("添加成功");
        }
    }

    @Override
    public ResultObject getCusUnitAreaById(Integer id) {
        CusUnitArea cusUnitArea = cusUnitAreaMapper.selectById(id);
        if (cusUnitArea != null){
            return ResultObject.success(cusUnitArea);
        }else {
            return ResultObject.success("没有这条数据");
        }
    }

    /**
     * 设置时间和用户的默认值
     * @param entity
     */
    private void setEntityValue(CusUnitArea entity) {
        Date currDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        entity.setCreatedOn(sdf.format(currDate));
        entity.setUpdatedOn(sdf.format(currDate));
        String userName = HttpUtil.getRequestHeaderInfo(Constants.USER_NAME);
        userName = userName == null || userName.isEmpty() ? "System" : userName;
        entity.setCreatedBy(userName);
        entity.setUpdatedBy(userName);
    }
}
